package com.ruoyi.jihuo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.jihuo.api.CallTaoApiService;
import com.ruoyi.jihuo.constant.TaoApiConstant;
import com.ruoyi.system.service.ISysConfigService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jihuo.mapper.JhTmTemplateMapper;
import com.ruoyi.jihuo.domain.JhTmTemplate;
import com.ruoyi.jihuo.service.IJhTmTemplateService;

/**
 * 模板配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@Service
public class JhTmTemplateServiceImpl implements IJhTmTemplateService
{
    @Autowired
    private JhTmTemplateMapper jhTmTemplateMapper;
    @Autowired
    private ISysConfigService iSysConfigService;
    @Autowired
    private CallTaoApiService callTaoApiService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询模板配置
     *
     * @param jhId 模板配置主键
     * @return 模板配置
     */
    @Override
    public JhTmTemplate selectJhTmTemplateByJhId(Long jhId)
    {
        return jhTmTemplateMapper.selectJhTmTemplateByJhId(jhId);
    }

    /**
     * 查询模板配置列表
     *
     * @param jhTmTemplate 模板配置
     * @return 模板配置
     */
    @Override
    public List<JhTmTemplate> selectJhTmTemplateList(JhTmTemplate jhTmTemplate)
    {
        jhTmTemplate.setJhUserId(SecurityUtils.getUserId());
        return jhTmTemplateMapper.selectJhTmTemplateList(jhTmTemplate);
    }

    /**
     * 新增模板配置
     *
     * @param jhTmTemplate 模板配置
     * @return 结果
     */
    @Override
    public int insertJhTmTemplate(JhTmTemplate jhTmTemplate)
    {
        jhTmTemplate.setJhUserId(SecurityUtils.getUserId());
        return jhTmTemplateMapper.insert(jhTmTemplate);
    }

    /**
     * 修改模板配置
     *
     * @param jhTmTemplate 模板配置
     * @return 结果
     */
    @Override
    public int updateJhTmTemplate(JhTmTemplate jhTmTemplate)
    {
        return jhTmTemplateMapper.updateJhTmTemplate(jhTmTemplate);
    }

    /**
     * 批量删除模板配置
     *
     * @param jhIds 需要删除的模板配置主键
     * @return 结果
     */
    @Override
    public int deleteJhTmTemplateByJhIds(String[] jhIds)
    {
        return jhTmTemplateMapper.deleteJhTmTemplateByJhIds(jhIds);
    }

    /**
     * 删除模板配置信息
     *
     * @param jhId 模板配置主键
     * @return 结果
     */
    @Override
    public int deleteJhTmTemplateByJhId(String jhId)
    {
        return jhTmTemplateMapper.deleteJhTmTemplateByJhId(jhId);
    }

    @Override
    public String getShopFreightTemplate(String session, String appkey, String secret) {
        try {
            String url = TaoApiConstant.TAOBAO_LOGISTICS_API_URL;
            String sessionKey = session;
            JSONObject jsonObject = callTaoApiService.freightTemplate(url, appkey, secret, sessionKey);
            JSONArray deliveryTemplates = jsonObject.getJSONObject("delivery_templates_get_response")
                .getJSONObject("delivery_templates")
                .getJSONArray("delivery_template");
            return deliveryTemplates.toJSONString();
        } catch (ApiException e) {
            throw new RuntimeException("获取店铺运费模版异常:"+e.getErrCode());
        }
    }

    @Override
    public List<Map<String, String>> getRedisShopFreightTemplate() {
        List<Map<String, String>> resultList = new ArrayList<>();
        Map<String, Object> allTemplates = redisCache.getCacheMap(CacheConstants.SHOP_FREIGHT_TEMPLATE);

        for (Map.Entry<String, Object> entry : allTemplates.entrySet()) {
            try {
                // 解析JSON字符串
                JSONArray templates = JSONArray.parseArray(entry.getValue().toString());
                for (int i = 0; i < templates.size(); i++) {
                    JSONObject template = templates.getJSONObject(i);
                    Map<String, String> templateMap = new HashMap<>();
                    templateMap.put("template_id", template.getString("template_id"));
                    templateMap.put("name", template.getString("name"));
                    resultList.add(templateMap);
                }
            } catch (Exception e) {
                // 处理解析异常
                e.printStackTrace();
            }
        }
        return resultList;
    }

}
